<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>我的信息</title>

    <!-- Bootstrap -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="./js/html5shiv.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->
    <script src="../dist/js/vue.js"></script>
    <script src="../dist/js/axios.js"></script>
    <link rel="stylesheet" href="./css/common.css">
    <script src="./js/common.js"></script>
    <style>
        .userInfo {
            background-color: #fff;
            padding: 30px;
            border-radius: 4px;
            margin-bottom: 30px;
        }

        .carts, .userAddresses {
            background-color: #fff;
            padding: 30px;
            border-radius: 4px;
            margin-bottom: 30px;
        }

        .addresses {
            margin: 60px 10px;
        }

        .solveNumber {
            display: inline-block;
            font-size: 14px;
            cursor: pointer;
            /*border: 1px solid black;*/
            /*border-radius: 4px;*/
            width: 15px;
            /*height: 10px;*/
            padding-right: 10px;
        }
    </style>
</head>
<body>

<div id="app">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <a class="navbar-brand" href="/views/client/index.html">MALL</a>
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">MALL</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <!--                <li class=""><a href="#">Link <span class="sr-only">(current)</span></a></li>-->
                    <!--                <li><a href="#">Link</a></li>-->

                </ul>
                <form class="navbar-form navbar-left">
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="./register.html" v-if="currentUser.id===undefined">注册</a></li>
                    <li><a href="./login.html" v-if="currentUser.id===undefined">登录</a></li>
                    <li class="dropdown" v-if="currentUser.id!==undefined">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">
                            <img :src="currentUser.avatar" class=" img-circle" style="width: 30px;height: 30px;" alt="">
                            {{currentUser.username}}
                            <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="./info.html">我的信息</a></li>
                            <li><a href="./cart.html">我的购物车</a></li>
                            <li><a href="./order.html">我的订单</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a @click="this.clientLogout()" href="#">退出</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>

    <div class="container">
        <ul class="nav nav-pills" style="margin:10px 0;">
            <li role="presentation" class="active"><a href="./info.html">我的信息</a></li>
            <li role="presentation"><a href="./cart.html">我购物车</a></li>
            <li role="presentation"><a href="./order.html">我的订单</a></li>
        </ul>
        <div class="userInfo">
            <h4>用户信息</h4>
            <div id="username" class="form-group">
                <label for="username">用户名</label>
                <input type="text" disabled class="form-control" :value="currentUser.username"/>
            </div>
            <div class="form-group">
                <label for="role">角色</label>
                <input type="text" disabled class="form-control" id="role" :value="currentUser.role">
            </div>
            <div class="form-group">
                <label>头像</label>
                <!--                <form action="/common/upload" method="post">-->
                <div class="avatar">
                    <img :src="currentUser.avatar" alt="" class="img-thumbnail" style="width: 140px;height: 140px;">
                    <input multiple @change="uploadAvatar" class="btn"
                           style="width: 140px;height: 140px;display: inline-block;border-radius: 4px;border:1px solid black;"
                           type="file"/>
                </div>
                <!--                    <div class="form-group">-->
                <!--                        <button type="submit" class="btn btn-success form-control">修改</button>-->
                <!--                    </div>-->
                <!--                </form>-->
            </div>
        </div>
        <div class="form userAddresses">
            <h4 class="title">收货地址</h4>
            <button type="button" class="btn btn-success" style="float:right;" data-target="#addAddressModel"
                    data-toggle="modal">添加收货地址
            </button>
            <div class="addresses" v-for="(address,index) of addresses" :key="address.id">
                <label>{{"收货地址" + index}}</label>
                <button class="btn btn-danger" @click="deleteAddressById(address.id)">删除</button>
                <div class="form-group  name">
                    <label :for="'name-'+index" class="">收货人 </label>
                    <input type="text" :id="'name-'+index" v-model="address.name" class="form-control">
                </div>
                <div class="form-group  mobile">
                    <label :for="'mobile-'+index" class="">联系方式</label>
                    <input type="text" :id="'mobile-'+index" v-model="address.mobile" class="form-control">
                </div>
                <div class="form-group  addressDetail">
                    <label :for="'addressDetail-'+index" class="">详细地址</label>
                    <input type="text" class="form-control" :id="'addressDetail-'+index"
                           v-model="address.addressDetail">
                </div>
            </div>
            <div class="form-group">
                <button type="button" class="form-control btn btn-success" @click="updateAddress()"
                        style="height: 45px;">
                    修改
                </button>
            </div>

            <div class="modal fade" tabindex="-1" id="addAddressModel" role="dialog">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title">添加收货地址</h4>
                        </div>
                        <div class="modal-body">
                            <div class="form">
                                <div class="form-group">
                                    <label for="newName">收货人</label>
                                    <input type="text" id="newName" v-model.trim="newAddress.name"
                                           class="form-control"/>
                                </div>
                                <div class="form-group">
                                    <label for="newMobile">联系方式</label>
                                    <input type="text" id="newMobile" class="form-control"
                                           v-model.trim="newAddress.mobile"/>
                                </div>
                                <div class="form-group">
                                    <label for="newAddressDetail">详细地址</label>
                                    <input type="text" id="newAddressDetail" class="form-control"
                                           v-model.trim="newAddress.addressDetail"/>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-success" @click="addAddress()">提交</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->
        </div>
    </div>
</div>

<footer class="main-footer">
    <strong>Copyright &copy; 2020 <a href="">MALL</a></strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
        <b>Version</b> v1.0.0
    </div>
</footer>
</body>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="./js/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="./js/bootstrap.min.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            currentUser: {},
            addresses: [],
            newAddress: {},


            checkedGoods: []
        },
        methods: {
            uploadAvatar(event) {
                const file = event.target.files[0];
                var forms = new FormData();
                forms.append('file', file);
                forms.append('type', 1);
                axios.post("/common/file/upload", forms, {
                    headers: {
                        'Content-type': 'multipart/form-data',
                        Authorization: localStorage.getItem("accessToken") != null ? "Bearer " + localStorage.getItem("accessToken") : ""
                    }
                }).then(response => {
                    console.log(response.data);
                    if (response.data.code === 200) {
                        retrieveUserByToken();
                    }
                }).catch(err => {
                    console.log(err);
                })
            },
            deleteAddressById: function (id) {
                axios.delete("/client/address/deleteAddressById/" + id, {
                    headers: {
                        Authorization: accessToken
                    }
                }).then(response => {
                    if (response.data.code === 200) {
                        retrieveUserAddressesByToken();
                    }
                }).catch(err => {
                    console.log(err);
                })
            },
            addAddress: function () {
                console.log(this.newAddress.addressDetail);
                if (this.newAddress.addressDetail === undefined || this.newAddress.addressDetail === "") {
                    alert("详细地址不应为空！");
                    return;
                }
                if (this.newAddress.name === undefined || this.newAddress.name === "") {
                    alert("收货人不应为空！");
                    return;
                }
                if (this.newAddress.mobile === undefined || this.newAddress.mobile === "") {
                    alert("联系方式不应为空！！");
                    return;
                }
                axios.post("/client/address/addAddress", {
                    userId: this.currentUser.id,
                    addressDetail: this.newAddress.addressDetail,
                    name: this.newAddress.name,
                    mobile: this.newAddress.mobile
                }, {
                    headers: {
                        Authorization: localStorage.getItem("accessToken") != null ? "Bearer " + localStorage.getItem("accessToken") : ""
                    }

                }).then(response => {
                    if (response.data.code === 200) {
                        retrieveUserAddressesByToken();
                        $('#addAddressModel').modal('hide');
                    }
                }).catch(err => {
                    console.log(err);
                })
            },
            updateAddress: function () {
                axios.post("/client/address/updateAddresses", this.addresses, {
                    headers: {
                        Authorization: accessToken
                    }
                }).then(response => {
                    if (response.data.code === 200) {
                        alert("修改地址成功！");
                    }
                    retrieveUserAddressesByToken();
                }).catch(err => {
                    console.log(err);
                })
            },
        },
        mounted: function () {
            retrieveUserByToken();
            retrieveUserAddressesByToken();
            // retrieveUserCarts(1, this.cartsPageInfo.pageSize);
        },
        computed: {}
    });
</script>

</html>